<template>
  <a-modal :open="open" width="750px" title="新增检验大类" centered :ok-button-props="{ disabled: !isCompleted }" @ok="onOk" @cancel="onCancel">
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="名称" class="w-1/2" v-bind="validateInfos.categroyName">
        <a-input v-model:value="formState.categroyName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="仪器类型" class="w-1/2">
        <a-select ref="select" v-model:value="formState.lisInstrumentType" class="w-52" placeholder="请选择">
          <a-select-option v-for="item in instrumentList" :key="item" :value="item.id">{{ item.instrumentTypeName }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/2">
        <a-input-number v-model:value="formState.outorder" class="w-full" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.cmemo" :rows="4" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const instrumentList = ref([])
const formState = ref({
  categroyName: '',
  yblx: null
})

const rulesState = ref({
  categroyName: [{ required: true, message: '请输入名称' }]
})

const { isCompleted, validateInfos } = useForm(formState, rulesState)

const getInstrumentTypeList = async () => {
  const { code, data, msg } = await post('/lis/InstrumentType/getInstrumentTypeList', {
    pageNum: 1,
    pageSize: 999,
    pyjm: '',
    categroyCode: '',
    status: 1
  })
  if (code !== 0) return message.error(msg)
  instrumentList.value = data
}

const onOk = async () => {
  const { code, msg } = await post('/lis/InstrumentType/saveLisCategroy', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
    }
  }
)

onMounted(() => {
  getInstrumentTypeList()
})
</script>
